package br.com.globalcode.clinicafacil.modelo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.globalcode.clinicafacil.config.Configurador;
import br.com.globalcode.clinicafacil.modelo.dao.exception.ConnectionFailureException;
import br.com.globalcode.clinicafacil.modelo.dao.exception.CreateDaoException;
import br.com.globalcode.clinicafacil.modelo.dao.exception.UpdateDaoException;
import br.com.globalcode.clinicafacil.modelo.entidade.Paciente;
import br.com.globalcode.framework.dao.GenericDao;

public class PacienteDao {
	
	private GenericDao genericDao;
	
	public PacienteDao() {
		genericDao = new GenericDao();
	}

	public void inserir(Paciente paciente) throws CreateDaoException {

		System.out.println("Inserindo paciente " + paciente);
		Object[] parameters = null;
		try {
			
			
			String sql = "insert into app.paciente (nome , cpf) values ( ? , ? )";
		
			genericDao.executePreparedUpdate(  sql
										    , paciente.getNome()
						                    , paciente.getCpf() );
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			
		}

		System.out.println("paciente inserido com sucesso ");

	}

	public void atualizar(Paciente paciente) throws UpdateDaoException {
		try {

			genericDao.executeUpdate("update  app.paciente set " + " nome ='"
					+ paciente.getNome() + "' , cpf='" + paciente.getCpf()
					+ "'" + " where id = " + paciente.getId());

		} catch (ClassNotFoundException e) {
			throw new UpdateDaoException(e);
		} catch (SQLException e) {
			throw new UpdateDaoException(e);
		}
	}

	public void delete(Paciente paciente) {
		// Registrar o Driver
		try {
			genericDao.executeUpdate(
					"delete from  app.paciente " + " where id = "
					+ paciente.getId());

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Paciente>  listarTodos ()  {
		
		List<Paciente> pacientes = new ArrayList<Paciente>();
		try {
			
			ResultSet rset = genericDao.executeQuery(
									"select * from app.paciente");

			while ( rset.next()  ) {
			
				Paciente paciente = new Paciente();
				paciente.setId( rset.getInt("id")  );
				paciente.setNome( rset.getString("nome")  );
				paciente.setCpf( rset.getString("cpf")  );
				
				
				pacientes.add(paciente);
			}
			
		} catch (ClassNotFoundException e) {
		} catch (SQLException e) {
		}
		
		return pacientes;
	}

	

}
